package com.lb.bs.client.util;

import com.lb.bs.client.beans.LBGrayIPBean;
import com.lb.bs.client.beans.LBGrayItemBean;
import com.lb.bs.client.repository.StoreCenter;
import com.lb.bs.client.constants.Constants;
import com.lb.bs.client.processor.enums.KeyType;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 * program: LB-BS-Client
 * author: bsworld.xie
 * create: 2019-12-18 17:23
 * description:
 */
public class GrayUtil {

    public static List<String> ipStr2List(String ipStr) {
        if (ipStr == null) {
            return Collections.emptyList();
        }
        String[] split = ipStr.split(Constants.GRAY_SPLIT_SYMBOL);
        if (split.length == 0) {
            return Collections.emptyList();
        }
        return Arrays.asList(split);
    }


    public static List<String> zkIpData2List(Object ipData) {

        String ipStr = FieldUtil.transform2String(ipData);

        return ipStr2List(ipStr);
    }

    public static boolean isGrayIpKey(String zkKey) {
        Map<String, LBGrayIPBean> grayMap = StoreCenter.getInstance().getGrayIPKeyMap();
        //是灰度ip key，走灰度策略
        if (grayMap.containsKey(zkKey)) {
            return true;
        }
        return false;
    }

    public static boolean isGrayItemKey(String zkKey) {
        Map<String, LBGrayItemBean> grayItemKeyMap = StoreCenter.getInstance().getGrayItemKeyMap();
        if (CollectionUtils.isNotEmpty(grayItemKeyMap) && grayItemKeyMap.containsKey(zkKey)) {
            return true;
        }
        return false;
    }

    public static boolean isLogPath(String zkKey) {
        Map<String, String> groupLogPathMap = StoreCenter.getInstance().getGroupLogPathMap();
        if (CollectionUtils.isEmpty(groupLogPathMap)) {
            return false;
        }
        if (groupLogPathMap.containsValue(zkKey)) {
            return true;
        }
        return false;
    }


    public static KeyType getKeyType(String zkKey) {

        return isGrayIpKey(zkKey) ? KeyType.GRAY_IP_KEY : isGrayItemKey(zkKey) ? KeyType.GRAY_ITEM_KEY : KeyType.ITEM_KEY;
    }



}

